﻿/**
*	Class: Project.js
*
*	Used to handle users actions involving the project elements.
*	
*	@param name - Project's name
*	@param startDate - Date when the project was created
*	@param finalizationDate - Date when the project is estimated to be finished
*/
function Project(name, startDate, finalizationDate){

	this.name = name;
	
	this.startDate = startDate;
	
	this.finalizationDate = finalizationDate;
	
    
    // Containers
    this.todo = new Container(TAG_TODO);
    this.doing = new Container(TAG_DOING);
    this.impeded = new Container(TAG_IMPEDED);
    this.done = new Container(TAG_DONE);
    
    this._containers = new Array(4);

    this._containers[TODO] = this.todo;
    this._containers[DOING] = this.doing;
    this._containers[IMPEDED] = this.impeded;
    this._containers[DONE] = this.done;

}

// Project methods

// Defining getters

/**
*	@return String - the project's name
*/
Project.prototype.getName = function(){
	return this.name;
};

/**
*	@return Date - the project's finalization Date
*/
Project.prototype.getFinalizationDate = function(){
	return this.finalizationDate;
};

//Defining setters

/**
*	@param name - the project's name
*/
Project.prototype.setName = function(name){
	this.name = name;
};


/**
*	@param finalizationDate - the project's estimated time to be accomplished
*/		
Project.prototype.setFinalizationDate = function(finalizationDate){
	this.finalizationDate = finalizationDate;
};

/**
*	@param container - the project's todo container
*/
Project.prototype.setTodo = function(container){
	this.todo = container;
	this._containers[TODO] = container;
};

/**
*	@param container - the project's doing container
*/		
Project.prototype.setDoing = function(container){
	this.doing = container;
	this._containers[DOING] = container;			
};

/**
*	@param container - the project's impeded container
*/
Project.prototype.setImpeded = function(container){
	this.impeded = container;
	this._containers[IMPEDED] = container;
};

/**
*	@param container - the project's done container
*/
Project.prototype.setDone = function(container){
	this.done = container;
	this._containers[DONE] = container;
};

/**
*	Inserts a post-it in the project.
*
*	@param postIt - post-it with all the new information
*	@param containerId - id of the container which the post-it is localized
*/
Project.prototype.addPostIt = function(postIt, containerId){
	this._containers[containerId].addPost(postIt);
};

/**
*	Alters the information in a existing post-it.
*
*	@param postIt - post-it with all the new information
*	@param containerId - id of the container which the post-it is localized
*/
Project.prototype.alterPostIt = function(postIt, containerId){
	var container = this._containers[containerId];
	container.removePost(postIt);
	container.addPost(postIt);
};

/**
*	Removes post-it from the project.
*
*	@param postIt - post-it which will be removed
*	@param containerId - id of the container which the post-it is localized
*/
Project.prototype.removePostIt = function(postIt, containerId){
	this._containers[containerId].removePost(postIt);
};

/**
*	Moves a post-it from one container to another.
*
*	@param postIt - post-it which will swap containers
*	@param oldContainer - id of the container which the post-it is localized
*	@param newContainer - id of the destiny container
*/
Project.prototype.alterContainer = function(postIt, oldContainer, newContainer){
	this._containers[oldContainer].removePost(postIt);
	this._containers[newContainer].addPost(postIt);
};

/**
*	Sort all the containers with the given sorting criteria.
*
*	@param sortCriteria - post-it compartion function
*/
Project.prototype.sortAll = function(sortCriteria) {
    for (var i = 0; this._containers[i]; i++) {
        this._containers[i].sortPosts(sortCriteria);
    }
};

/**
*	Generates an xml representation of the project.
*
*	@return String - the project in xml format
*/
Project.prototype.genXml = function(){

	var xml = String("");
	
	xml += "<" + TAG_PROJECT  + ">";

	xml += tag(TAG_NAME, this.name);
	
	xml += "<" + TAG_START_DATE + ">";			
	xml += tag(TAG_DAY, this.startDate.getDate());
	xml += tag(TAG_MONTH, this.startDate.getMonth());
	xml += tag(TAG_YEAR, this.startDate.getFullYear());
	xml += "</" + TAG_START_DATE + ">";
	
	xml += "<" + TAG_FINALIZATION_DATE + ">";
	xml += tag(TAG_DAY, this.finalizationDate.getDate());
	xml += tag(TAG_MONTH, this.finalizationDate.getMonth());
	xml += tag(TAG_YEAR, this.finalizationDate.getFullYear());
	xml += "</" + TAG_FINALIZATION_DATE + ">";
			
	xml += this.todo.genXml();
	xml += this.doing.genXml();
	xml += this.impeded.genXml();
	xml += this.done.genXml();
	
	xml += "</" + TAG_PROJECT + ">";
	
	return xml;
};

